package com.chixing.service.impl;


import com.chixing.mapper.HotelMapper;
import com.chixing.pojo.Hotel;
import com.chixing.pojo.HotelExample;
import com.chixing.service.HotelService;
import com.chixing.util.ServerResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class HotelServiceImpl implements HotelService {
    @Autowired
    private HotelMapper hotelMapper;

    @Override
    public ServerResponse getById(Integer id) {
        Hotel hotel = hotelMapper.selectByPrimaryKey(id);
        if (hotel != null)
            return ServerResponse.getSuccess(hotel);
        else
            return ServerResponse.getFail(null);
    }

    @Override
    public ServerResponse getAll() {
        List<Hotel> hotelList = hotelMapper.selectByExample(null);
        if (hotelList != null && hotelList.size()>0)
            return ServerResponse.getSuccess(hotelList);
        return ServerResponse.getFail(null);

    }


    //多条件查询
    //表Hotel
    //房屋类型 ，户型，房屋位置 房屋
    @Override
    public ServerResponse getByConditions(String hotelArea, String hotelType, Float minPrice, Float maxPrice,String roomType ,Integer pageNum) {
        HotelExample hotelExample = new HotelExample();
        HotelExample.Criteria hotelCriteria = hotelExample.createCriteria();

        if(hotelArea!=null && hotelArea.equals("-1")  )
            hotelArea=null;
       if(hotelType!=null && hotelType.equals("-1")  )
            hotelType = null;

        if(minPrice!=null && minPrice == -1)
            minPrice=null;
        if(maxPrice!=null && maxPrice == -1)
            maxPrice=null;
        if(roomType!=null && roomType.equals("-1")  )
            roomType=null;
        if (hotelArea != null) {
            hotelCriteria.andHotelAreaEqualTo(hotelArea);
        }
        if (hotelType != null) {
            hotelCriteria.andHotelTypeEqualTo(hotelType);
        }
        if (minPrice != null) {
            hotelCriteria.andHotelPriceGreaterThanOrEqualTo(minPrice);
        }
        if (maxPrice != null) {
            hotelCriteria.andHotelPriceLessThanOrEqualTo(maxPrice);
        }
        if (roomType != null) {
          hotelCriteria.andRoomTypeEqualTo(roomType);
        }

     PageHelper.startPage(pageNum,6);
        List<Hotel> hotelList =  hotelMapper.selectByExample(hotelExample);
        PageInfo<Hotel> pageInfo = new PageInfo<>(hotelList);
        System.out.println("hotellist"+hotelList);
        if(hotelList != null && hotelList.size()>0){
            return ServerResponse.getSuccess(pageInfo);
        }else{
            return  ServerResponse.getFail(null);
        }
    }

}
